﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RadarMonitor.FTPMonitor;
using RadarMonitor.DB;
using RadarMonitor;
using RadarMonitor.Alarm;

namespace RadarMonitor.ViewModel
{
    class BaseFTPViewModel : INotifyPropertyChanged
    {
        private Setting.BaseFTPConfig config;
        public BaseFTPViewModel()
        {
            config = Setting.Setting.GetConfig().baseFTP;
            try
            {
                var list = DBHelper.Instance.lastRow(DBHelper.BaseFTPTabel);
                LastCheckTime = (DateTime)list[1];
                //ResultCode = (int)list[3];
            }
            catch { }
            BaseFtpMonitor.Instance.monitor.FTPAccessFailedEvent += Monitor_FTPAccessFailedEvent;
            BaseFtpMonitor.Instance.monitor.RadarDataTransmissionSuccessed += Monitor_RadarDataTransmissionSuccessed;
            BaseFtpMonitor.Instance.monitor.RadarDataTransmissionTimeoutEvent += Monitor_RadarDataTransmissionTimeoutEvent;
            
        }
       

        private void Monitor_RadarDataTransmissionTimeoutEvent(DateTime? arg1, string arg2)
        {
            if (arg2!=null)
            {
                DBHelper.Instance.insertBaseFTPRow(arg2, (int)(RadarMonitorResultCode.FTPFileUploadFailed));
                DBHelper.Instance.insertMonidorRow((int)(RadarMonitorSource.BaseFTP), (int)(RadarMonitorResultCode.FTPFileUploadFailed),null);
                ResultCode = (int)(RadarMonitorResultCode.FTPFileUploadFailed);
                AlarmManager.Alarm(RadarMonitorSource.BaseFTP, RadarMonitorResultCode.FTPFileUploadFailed, null,config.DingDingEnabel,config.SmsEnabel,config.AudioEnabel);
            }
            else 
            {
                DBHelper.Instance.insertBaseFTPRow(null, (int)(RadarMonitorResultCode.FTPFileNotExist));
                DBHelper.Instance.insertMonidorRow((int)(RadarMonitorSource.BaseFTP), (int)(RadarMonitorResultCode.FTPFileNotExist), null);
                ResultCode = (int)(RadarMonitorResultCode.FTPFileNotExist);
                AlarmManager.Alarm(RadarMonitorSource.BaseFTP, RadarMonitorResultCode.FTPFileNotExist, null, config.DingDingEnabel, config.SmsEnabel, config.AudioEnabel);
            }

            LastCheckTime = DateTime.Now;
        }

        private void Monitor_RadarDataTransmissionSuccessed(string file)
        {
            DBHelper.Instance.insertBaseFTPRow(file, (int)(RadarMonitorResultCode.Success));
            LastCheckTime = DateTime.Now;
            ResultCode = (int)(RadarMonitorResultCode.Success);
        }

        private void Monitor_FTPAccessFailedEvent(Exception ex)
        {
            LastCheckTime = DateTime.Now;
            ResultCode = (int)(RadarMonitorResultCode.FTPAcesssFailed);

            DBHelper.Instance.insertBaseFTPRow(null, (int)(RadarMonitorResultCode.FTPAcesssFailed));
            DBHelper.Instance.insertMonidorRow((int)(RadarMonitorSource.BaseFTP), (int)(RadarMonitorResultCode.FTPAcesssFailed), ex.Message);
            AlarmManager.Alarm(RadarMonitorSource.BaseFTP, RadarMonitorResultCode.FTPAcesssFailed, ex.Message, config.DingDingEnabel, config.SmsEnabel, config.AudioEnabel);
        }

        #region DataContext
        private bool _isWorking;

        public bool IsWorking
        {
            get { return _isWorking; }
            set { _isWorking = value;
                OnPropertyChanged("IsWorking");
            }
        }

        private DateTime _lastCheckTime = DateTime.MinValue;

        public DateTime LastCheckTime
        {
            get { return _lastCheckTime; }
            set { _lastCheckTime = value;
                OnPropertyChanged("LastCheckTime");
            }
        }

        private int _resultCode;

        public int ResultCode
        {
            get { return _resultCode; }
            set { _resultCode = value;
                OnPropertyChanged("ResultCode");
            }
        }


        #endregion

        #region Action
        public void start()
        {
            BaseFtpMonitor.Instance.Start();
            IsWorking = true;
        }

        public void stop()
        {
            BaseFtpMonitor.Instance.Stop();
            IsWorking = false;
        }

        public void toogle()
        {
            if (IsWorking)
            {
                stop();
            }
            else
            {
                start();
            }
        }


        #endregion


        #region 属性变化通知
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName = "")
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }



        #endregion
    }
}
